package cn.biplam.front.component.view.systemManage.sysprivi.role.manage.del;

import cn.biplam.common.bean.md.IndexTable;
import cn.biplam.common.bean.systemManage.privi.RoleManageBean;
import cn.biplam.common.bean.tree.IndexTableExt;
import cn.biplam.common.paramters.SysCaption;
import cn.biplam.common.paramters.SysName;
import cn.biplam.common.paramters.TreeParam;
import cn.biplam.common.struc.SysStruc.RoleIndexStruc;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.front.component.ComponentBase;
import cn.biplam.front.component.data.common.CaptionComp;
import cn.biplam.front.component.view.systemManage.sysprivi.role.RoleManage;
import cn.biplam.front.component.view.systemManage.sysprivi.role.manage.navi.RoleNavi;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Window_Event;
import cn.biplam.front.htmlscript.script.PopupScript;
import cn.biplam.front.htmlscript.script.ScriptTreeNew;
import cn.biplam.front.template.base.Text;
import cn.biplam.front.template.control.Confirm;

/**
 *  删除角色操作
 * @author XiaoRuxing Dec 4, 200911:33:23 AM
 * @version 1.0
 */
public class DelRole extends ComponentBase{

	public DelRole(UserInfo user, String homeUrl, String recordId,
			KvMap<String, String> params) {
		super(user, homeUrl, recordId, params);
	}

	/**
	 *  删除角色主题界面
	 * @return 删除角色主题界面
	 * @throws ErrorMessage 
	 */
	public HtmlWindow delRoleTheme() throws ErrorMessage{
		String recordId = params.getByKey(TreeParam.NODE_ID);
		//1.获取角色主题是否有下级
		IndexTableExt boBean = IndexTableExt.getNewInstance(mUser.getUserID(),RoleIndexStruc.Table_Name);
		//2.角色名称
		IndexTable table = IndexTable.getNewInstance(mUser.getUserID(), RoleIndexStruc.Table_Name, recordId);
		String roleName = table.getNodeName();
		if(roleName==null)roleName = recordId;
		
		HtmlWindow div;
		if(boBean.getChildrenList(recordId)==null){//是否有下级
			/**创建删除提示*/
			StringBuilder sb = new StringBuilder();
			sb.append(CaptionComp.get(mUser, SysCaption.Confirm));
			sb.append(CaptionComp.get(mUser, SysCaption.Delete));
			sb.append(" ");
			sb.append(roleName);
			sb.append(" ?");
			
			/**生成提示信息窗口*/
			Confirm delete = new Confirm(sb.toString(),recordId);
			delete.putAddParams(TreeParam.NODE_CONTARINER_ID, 
					params.getByKey(TreeParam.NODE_CONTARINER_ID));
			delete.bind(RoleManage.Role_Dir_Del_Submit);
			div=delete.get();
			
		}else{//有下级
			StringBuilder sb = new StringBuilder();
			sb.append(CaptionComp.get(mUser, SysCaption.Delete));
			sb.append(" ");
			sb.append(roleName);
			sb.append(" ");
			sb.append(CaptionComp.get(mUser, SysCaption.Cancel));
			Confirm delete = new Confirm(sb.toString(),recordId);
			HtmlWindow confirm=new Text(CaptionComp.get(mUser, SysCaption.CantDeleteTreeNode)).get();
			confirm.setFontColor("red");
			delete.put(confirm);
			div=delete.get();
			div.bind(SysName.PromptSubmitID,Window_Event.OnClick,PopupScript.cancel(SysName.PromptID));
		}
		return div;
	}
	
	/**
	 *  删除角色界面
	 * @return 删除角色界面
	 * @throws ErrorMessage 
	 */
	public HtmlWindow panel() throws ErrorMessage{
		return delPanel(RoleManage.Role_Del_Submit);
	}
	
	/**
	 *  删除提示界面
	 * @param submitActId 删除提交动作ID
	 * @return 删除界面
	 */
	private HtmlWindow delPanel(String submitActId) throws ErrorMessage{
		String recordId = params.getByKey(TreeParam.NODE_ID);
		IndexTable table = IndexTable.getNewInstance(mUser.getUserID(), RoleIndexStruc.Table_Name, recordId);
		String roleName = table.getNodeName();
		if(roleName==null)roleName = recordId;
		
		StringBuilder sb = new StringBuilder();
		sb.append(CaptionComp.get(mUser, SysCaption.Confirm));
		sb.append(CaptionComp.get(mUser, SysCaption.Delete));
		sb.append(" ");
		sb.append(roleName);
		sb.append(" ?");
		
		/**生成提示信息窗口*/
		HtmlWindow win = null;
		Confirm delete = new Confirm(sb.toString(),recordId);
		delete.bind(submitActId);
		delete.putAddParams(TreeParam.NODE_CONTARINER_ID, 
				params.getByKey(TreeParam.NODE_CONTARINER_ID));
		win=delete.get();
		return win;
	}
	
	/**
	 *  删除角色提交操作
	 * @return 删除后刷新脚本
	 * @throws ErrorMessage 
	 */
	public HtmlWindow submit() throws ErrorMessage{
		RoleManageBean roleM = RoleManageBean.getInstance(mUser.getUserID());
		//删除角色
		roleM.delRole(recordId, false);
		//返回刷新脚本
		String script = ScriptTreeNew.getRefreshDelete(params.getByKey(TreeParam.NODE_CONTARINER_ID), 
				RoleNavi.Expand);
		HtmlWindow win = new HtmlDiv();
		win.bind(script);
		return win;
	}
	
	/**
	 *  删除角色主题提交操作
	 * @return 删除后刷新脚本
	 * @throws ErrorMessage 
	 */
	public HtmlWindow themeSubmit() throws ErrorMessage{
		RoleManageBean roleM = RoleManageBean.getInstance(mUser.getUserID());
		//删除角色
		roleM.delRoleTheme(recordId);
		//返回刷新脚本
		String script = ScriptTreeNew.getRefreshDelete(params.getByKey(TreeParam.NODE_CONTARINER_ID), 
				RoleNavi.Expand);
		HtmlWindow win = new HtmlDiv();
		win.bind(script);
		return win;
	}
}
